var kind;
var url;
$(document).ready(function(){
	var stationList=$('#stationList').datagrid({
				fit:true,
				url:js+'station/viewStation.action',
				sortName: 'stationID',
				idField:'stationID',
				fitColumns: true,
				height: 'auto',
				columns:[[{field:'ck',checkbox:true},
					{field:'stationName',title:'名称',width:120,sortable:true},
					{field:'stationCode',title:'编号',width:120,sortable:true},
					{field:'manager',title:'负责人',width:120,sortable:true},
					{field:'cityName',title:'地域城市',width:80,sortable:true,editor:'text'},
					{field:'address',title:'地址',width:220,sortable:true},
					{field:'contact',title:'联系人',width:150},
					{field:'telephone',title:'联系方式',width:150},
					{field:'handlerName',title:'创建者',width:150},
					{field:'handlerTime',title:'创建时间',width:150},
					{field:'operatorName',title:'最后修改者',width:150},
					{field:'operateTime',title:'最后修改时间',width:150}
				]],
				pagination:true,
				rownumbers:true,
				onDblClickRow:function(rowIndex, rowData ){
					if(isCityNameClick){
						$('#cityName').combogrid('setText',rowData.cityName);
					}
					$('#addStation').dialog('open').dialog('setTitle','修改网点信息');
					$('#addStation').form('load',rowData);
					url = js+'station/updateStation.action';
				}
			});


	$('#add').click(function(){
		kind='add';
		$('#addStation').dialog('open').dialog('setTitle','新增网点');
		$('#addStation').form('clear');
		url = js+'station/addStation.action';
		return false;
	})	
	$('#addStation').dialog({
				toolbar:[{
					text:'保存',
					iconCls:'icon-save',
					handler:function(){
						$('#addStationForm').form('submit',{
		    				url: url,
		    				onSubmit: function(){
			    				if($('#cityID').val()==null||$('#cityID').val()==''){
			    					$.messager.alert('提示：','请选择地域城市','error');
			    					return false;
			    				}
		    					 if($(this).form('validate')){
		    					 	return true;
		    					 }else{
		    					 	$.messager.alert('提示：','请填写完整信息','error');
		    					 	return false;
		    					 }
		    				},
		    				success: function(data){
		    					var result = eval('('+data+')');
		    					if (result.success){
		    						$('#stationList').datagrid('reload');
		    						$('#addStation').dialog('close');
		    						$('#addStationForm').dialog('clear');
		    						
		    						
		    					} else {
	    							$.messager.alert('提示：','网点编号重复','error');
		    					}
		    				}
		    			});
					}
				},'-',
				{
					text:'取消',
					iconCls:'icon-cancel',
					handler:function(){
						$('#addStation').dialog('close');
						$('#addStationForm').dialog('clear');
					}
				}
				]
				
				
			});	
			
	/*删除用户*/
	$("#detele").click(function(){
		var idsArray = new Array();
		var rows = $('#stationList').datagrid('getSelections');
		if(rows.length==0){
			$.messager.alert("提示","请选中数据","warning");
			return false;
		}
		$.messager.confirm("提示！","确定要删除选中的网点?",function(t){ 
			if(!t) return;
			for(var i=0;i<rows.length;i++){
				idsArray.push(rows[i].stationID);
			}	
			var ids= idsArray.join(",");
			$.post(js+"station/deleteByIDsStation.action",{ids:ids},function(result){
			     if(result.success){
			     	var rows = $('#stationList').datagrid('getSelections');
			     	while(rows&&rows.length>0){
						var index = $('#stationList').datagrid('getRowIndex', rows[0]);
						$('#stationList').datagrid('deleteRow', index);
						rows = $('stationList').datagrid('getSelections');
					}
			      }else{
			        $.messager.alert('提示',"删除不成功","error");	
			      }
			},"json");
			
		});
		
		return false;
	});
})
function lookup(value,name){
	var searchUrl;
	if('all'==name){
		searchInput='';	
		searchUrl = js+'station/viewStation.action?searchInput='+value;
	}else if('stationCode'==name){
		searchUrl = js+'station/viewStation.action?searchInput='+value+"&searchKind=stationCode";
	}else if('stationName'==name){
		searchUrl = js+'station/viewStation.action?searchInput='+value+"&searchKind=stationName";
	}
	$('#stationList').datagrid({
		 url:searchUrl,
    });
    $('#stationList').datagrid('reload');
}